var map;
	$(function(){
		for(var n in memberCardManagementJson){
			$('input[name="memberCardManagement.'+n+'"]').val(memberCardManagementJson[n]);
		}
		if(memberCardManagementJson.huiyuankaDescs.length>0){
			var huiyuanHtml = "";
			var newHuiyuanHtml = "";
				for(var i=0;i<memberCardManagementJson.huiyuankaDescs.length;i++){
					huiyuanHtml = "<ul class='ulClass'>";
						huiyuanHtml += "<li>";
							huiyuanHtml += "<table width='50%'>";
							huiyuanHtml += "<tr>";
							huiyuanHtml += "<td>";
							huiyuanHtml += "<input type='text' name='descTitle' value='"+memberCardManagementJson.huiyuankaDescs[i].descTitle+"'  class='form-control' placeholder='说明标题' style='width:596px;margin-left:-1px;'>";
							huiyuanHtml += "</td>";
							huiyuanHtml += "<td>";
							huiyuanHtml += "<a class='red-bg btn btn-sm' style='margin-left: 15px;' onclick='deleteHuiyuanka(this)'>移除</a>";
							huiyuanHtml += "</td>";
							huiyuanHtml += "</tr>";
							huiyuanHtml += "</table>";
						huiyuanHtml += "</li>";
						huiyuanHtml += "<li>";
							huiyuanHtml += "<textarea rows='' cols='' name='descTextarea' class='form-control' style='width:596px;height:90px;' placeholder='说明内容'>"+memberCardManagementJson.huiyuankaDescs[i].descTextarea+"</textarea>";
						huiyuanHtml += "</li>";
					huiyuanHtml += "</ul>";
					newHuiyuanHtml += huiyuanHtml;
				}
			$("#toHuiyuankaHtml").html(newHuiyuanHtml);				
		}else{
			$("#toHuiyuankaHtml").html($("#huiyuankaId").html());
		}
		
		if(memberCardManagementJson.storeAddrs.length>0){
			var storeAddrHtml = "";
			var newStoreAddrHtml = "";
			for(var i=0;i<memberCardManagementJson.storeAddrs.length;i++){
				storeAddrHtml = "<table width='50%' style='float: left;'>";
					storeAddrHtml += "<tr>";
						storeAddrHtml += "<td>";
							storeAddrHtml += "<input type='text' name='storeName' value='"+memberCardManagementJson.storeAddrs[i].storeName+"' class='form-control' placeholder='门店名称' style='width:161px;'>";
						storeAddrHtml += "</td>";
						storeAddrHtml += "<td>";
							storeAddrHtml += "<div class='input-group input-group' style='width:310px;'>";
								storeAddrHtml += "<input type='text' name='storeAddress' value='"+memberCardManagementJson.storeAddrs[i].storeAddress+"' readonly='readonly' class='form-control' placeholder='门店地址'>";
								storeAddrHtml += "<span class='input-group-addon btn-info' onclick='showBMap(this)' style='cursor: pointer;'><img src='../img/addr.png' style='width: 15px;height: 15px;' /></span>";			
								storeAddrHtml += "<input type='hidden' name='storeLongitude' value='"+memberCardManagementJson.storeAddrs[i].storeLongitude+"'>";
								storeAddrHtml += "<input type='hidden' name='storeLatitude' value='"+memberCardManagementJson.storeAddrs[i].storeLatitude+"'>";
							storeAddrHtml += "</div>";
						storeAddrHtml += "</td>";
						storeAddrHtml += "<td>";
							storeAddrHtml += "<input type='text' name='storeTelephone' value='"+memberCardManagementJson.storeAddrs[i].storeTelephone+"' class='form-control' placeholder='门店电话' style='width:120px;'>";
						storeAddrHtml += "</td>";
						storeAddrHtml += "<td>";
							storeAddrHtml += "<input type='text' name='storePassword' value='"+memberCardManagementJson.storeAddrs[i].storePassword+"' class='form-control' placeholder='门店密码' style='width:120px;'>";
						storeAddrHtml += "</td>";
						storeAddrHtml += "<td>";
							storeAddrHtml += "<a class='red-bg btn btn-sm ' style='margin-left: 15px;' onclick='deleteStoreAddr(this)'>移除</a>";
						storeAddrHtml += "</td>";
					storeAddrHtml += "</tr>";
				storeAddrHtml += "</table>";
				newStoreAddrHtml += storeAddrHtml;
			}
			$("#addtoStoreaddr").append(newStoreAddrHtml);
		}else{
			$("#addtoStoreaddr").append($("#storeandAddrId").html());
		}
		
		
		var imgUrl=memberCardManagementJson.imgUrl;
		$("#checkImg a").click(function(){
			var $this=$(this);
			$('#checkImg a.cur').removeClass('cur');
			$this.addClass('cur');
			$("#memberCardManagement_imgUrl").val($(" .cur img").attr("src"));
		});
		
		$('img[src="'+imgUrl+'"]').first().parent().click();
		
		var imgValue = $("#memberCardManagement_imgUrl").val();
		var $abtns = $(".abtn");
		if(imgValue==""){
			$("#memberCardManagement_imgUrl").val($(".abtn :eq(0) img").attr("src"));
		}else{
			$('#checkImg a.cur').removeClass('cur');
			var $chaifen = $("#memberCardManagement_imgUrl").val().split("/");
			if($chaifen[1]=="file"){
				$("#uploadImg").attr("src",$("#memberCardManagement_imgUrl").val());
				$("#uploadA").show();
				$("#uploadA").addClass("cur");
			}else{
				for(var i=0;i<$abtns.length;i++){
					if($($abtns[i]).find("img").attr("src")==imgValue){
						$($abtns[i]).addClass("cur");
					}
				}
			}
		}
		// 百度地图API功能
		map = new BMap.Map('map');    
		map.centerAndZoom(new BMap.Point(116.404, 39.915), 11);
		map.addControl(new BMap.NavigationControl()); // 添加平移缩放控件
		map.addControl(new BMap.ScaleControl()); // 添加比例尺控件
		map.addControl(new BMap.OverviewMapControl()); //添加缩略地图控件
		map.enableScrollWheelZoom(); // 启用滚轮放大缩小。
		map.addEventListener("click", function(e){
			map.clearOverlays();
			var marker1 = new BMap.Marker(new BMap.Point(e.point.lng, e.point.lat));  // 创建标注
			$("#longitude").val(e.point.lng);
			$("#latitude").val(e.point.lat);
			map.addOverlay(marker1); 
			$.ajax({
				url:'../member/getJsonStoreAndAddr.json?lat='+e.point.lat+'&lng='+e.point.lng,
				type:'post',
				dataType:'json',
				success:function(json){
					$('#formatted_address').val(json.result.formatted_address);
				}
			});
		});
	});

	function ajaxUploadMember(){
		$.ajaxFileUpload({
		url:"../file/upload.json",
		secureuri:false,
		fileElementId:"uploadify",
		dataType:"json",
		success:function(attach,status){
		 	$("#uploadImg").attr("src","../file/download.json?fileId="+attach.id);
		 	$("#memberCardManagement_imgUrl").val("../file/download.json?fileId="+attach.id);
		 	$("#uploadA").show();
		 	$('#checkImg a.cur').removeClass('cur');
		 	$("#uploadA").addClass("cur");
		},
		error:function(data,status,e){
		alert(e);
		}
	});
	return false;
	}
	
	//增加门店信息
	function addToStoreAddr(){
		var storeAddrHtml = $("#storeandAddrId").html();
		$("#addtoStoreaddr").append(storeAddrHtml);
	}
	//删除一条门店信息
	function deleteStoreAddr(obj){
		$(obj).parent().parent().remove();
	}
	//增加会员卡信息
	function addToHuiyuanka(){
		var huiyuankaHtml = $("#huiyuankaId").html();
		$("#toHuiyuankaHtml").append(huiyuankaHtml);
	}
	//删除一条会员卡信息
	function deleteHuiyuanka(obj){
		$(obj).parent().parent().parent().parent().parent().parent().remove();
	}
	function searchMap(){
		var local = new BMap.LocalSearch("全国", {
		  renderOptions: {
		    map: map,
		    panel : "r-result",
		    autoViewport: true,
		    selectFirstResult: false
		  }
		});
		local.search($('#keyword').val());
	}
	function saveMemberCardManagement(){
		$("#editMemberCardFrom").ajaxSubmit({
			url:'../member/saveMemberCardManagement.json',
			type:'post',
			dataType:'json',
			success:function(json){
				if(json.success){
					alert("保存成功");
				}else{
					alert("保存失败");
				}
			}
		});		
	}
	var dialog;
	function showBMap(obj){
		var $this = $(obj);
		var buttons = {
			"确定":function(){	
					$this.parent().find("input[name='storeLongitude']").val($("#longitude").val());
					$this.parent().find("input[name='storeLatitude']").val($("#latitude").val());
					$this.parent().find("input[name='storeAddress']").val($('#formatted_address').val());
					$(this).dialog("close");
				},
				"取消":function(){
					$(this).dialog("close");
				}
		};
		/**
			if(dialog){
			dialog.dialog("open");
			return;
		}
		 */
		dialog=$("#memberDialog").dialog({
			modal:true,
			width:700,
			height:550,
			buttons:buttons
		});
	}
	
function getAddressLngLat(obj){
	var $this = $(obj);
	var buttons = {
			"确定":function(){	
				$this.parent().find("input[name='memberCardManagement.longitude']").val($("#longitude").val());
				$this.parent().find("input[name='memberCardManagement.latitude']").val($("#latitude").val());
				$this.parent().find("input[name='memberCardManagement.address']").val($('#formatted_address').val());
				$(this).dialog("close");
			},
			"取消":function(){
				$(this).dialog("close");
			}
	};
	dialog=$("#memberDialog").dialog({
			modal:true,
			width:700,
			height:550,
			buttons:buttons
		});
}